package com.wu.store.config;

import com.wu.store.dao.UserDao;
import com.wu.store.entity.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomRealm extends AuthorizingRealm {
	{
		HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
		hashedCredentialsMatcher.setHashAlgorithmName("md5");
		this.setCredentialsMatcher(hashedCredentialsMatcher);
	}

	public CustomRealm() {
		super();
	}

	@Autowired
	private UserDao userDao;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		String username = (String) authenticationToken.getPrincipal();
		User user = userDao.getUserByUserName(username);
		if (user == null) {
			throw new UnknownAccountException("该账号不存在");
		}
		return new SimpleAuthenticationInfo(user, user.getPassword(), "customRealm");
	}

}
